//MINI UI解析
mini.parse();

//模板处理器类
function EditTplHandler() {
	this.data = {};
	this.saved = false;
	this.refresh = null;
	this.name = mini.get("name");
	this.form = new mini.Form("editTplForm");
	this.ckeditor = CKEDITOR.replace("content", { skin : "v2" });
	
	this.load = function(data, refresh) {
		data = mini.clone(data);
		this.data.iscreate = data.iscreate;
		this.data.parent = data.parent;
		this.refresh = refresh;
		
		if (!data.iscreate) {
			this.name.readOnly = true;
			
			var param = { forres : false, name : data.name, parent : data.parent };
			var json = mini.encode(param);
			$.ajax({
				url : __root + "/getContent.do", 
				type : "POST", 
				data : { data : json }, 
				dataType : "json", 
				success : function(result) {
					if (result.issuccess) {
						instance.form.setData(data);
						instance.ckeditor.setData(result.message);
					} else {
						mini.alert(result.message, "系统提示");
					}
				}, 
				error : function (request) {
					mini.alert(request.responseText, "系统提示");
				}
			});
		}
	};
	
	this.save = function() {
		this.form.validate();
		if (this.form.isValid()) {
			var data = this.form.getData();
			data.forres = false;
			data.iscreate = this.data.iscreate;
			data.parent = this.data.parent;
			data.content = this.ckeditor.getData();
			
			var json = mini.encode(data);
			$.ajax({
				url : __root + "/editContent.do", 
				type : "POST", 
				data: {
		        	data: json,
		        	lslUser: gbOperateUser,
		    		lslAddress: gbOperateIp
		        },
				dataType : "json", 
				success : function(result) {
					if (result.issuccess) {
						instance.saved = true;
						instance.data.iscreate = false;
						instance.name.readOnly = true;
					}
					mini.alert(result.message, "系统提示");
				}, 
				error : function (request) {
					mini.alert(request.responseText, "系统提示");
				}
			});
		}
	};
	
	this.close = function(action) {
		if (CloseOwnerWindow) {
			CloseOwnerWindow(action);
		} else {
			window.close();
		}
	};
	
	this.onclose = function() {
		if (instance.refresh && instance.saved) {
			instance.refresh();
		}
	};
};

//模板处理器全局实例
var instance = new EditTplHandler();

//定义保存按钮点击事件处理程序
function onSaveClick(e) {
	instance.save();
}

//定义关闭按钮点击事件处理程序
function onCancelClick(e) {
	if (instance.saved) {
		instance.close("save");
	} else {
		instance.close("cancel");
	}
}
